package com.ktgj.service.controller;

import com.ktgj.common.core.constant.FileConstants;
import com.ktgj.common.core.utils.StringUtils;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.service.domain.Contract;
import com.ktgj.service.domain.ContractFile;
import com.ktgj.service.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/contract")
public class ContractController extends BaseController
{
    @Autowired
    private IContractService contractService;

    /**
     * 查询合同列表
     */
    //@PreAuthorize(hasPermi = "service:contract:list")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract)
    {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @PreAuthorize(hasPermi = "service:contract:export")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contract contract) throws IOException
    {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "合同数据");
    }

    /**
     * 获取合同详细信息
     */
    //@PreAuthorize(hasPermi = "service:contract:query")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId)
    {
        return AjaxResult.success(contractService.selectContractById(contractId));
    }

    /**
     * 新增合同*/
    @PreAuthorize(hasPermi = "service:contract:add")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract)
    {
        //当页面新增合同信息成功之后，返回新增后的主键值，如果有合同附件，则调用合同附件上传功能(需要传递合同id、状态)
        return toAjax(contractService.insertContract(contract));
    }



    /**
     * 删除合同*/
    @PreAuthorize(hasPermi = "service:contract:remove")
    @Log(title = "合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds)
    {
        return toAjax(contractService.deleteContractByIds(contractIds));
    }


    /**
     * 根据服务来源、服务项目，联合查询 合作商(供应商/代理商)、合同数据
     * @param parinerName 服务来源
     * @param invoiceProject 服务项目名称
     * @return
     */
    @GetMapping(value = "getContractByContractAndInvoiceProject/{parinerName}/{invoiceProject}")
    public TableDataInfo getContractByContractAndInvoiceProject(@PathVariable("parinerName") String parinerName,
                                                                @PathVariable("invoiceProject") String invoiceProject)
    {
        startPage();
        List<Contract> list = contractService.selectContractAndParinerList(parinerName,invoiceProject);
        return getDataTable(list);
    }

    /**
     * 上传合同附件
     * @param file 文件
     * @param request
     * @param contractId 合同id
     * @param status 状态
     * @return
     */
    @PostMapping("/importFileContract")
    public AjaxResult importFileContract(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                                         @RequestParam("contractId")Long contractId,
                                         @RequestParam("status")String status) {
        //当页面合同附件不为空的时候走这个逻辑，传递合同id、状态
        boolean isFlag = false;
        for (String type : FileConstants.IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), type)) {
                isFlag = true;
                break;
            }
        }
        if (isFlag) {
            ContractFile contractFile = contractService.importFileContract(file, request,contractId,status);
            boolean isLegal = contractFile.isLegal();
            if (isLegal) {
                Map resultMap = new HashMap();
                resultMap.put("contractFilePath", contractFile.getIsPath());
                return AjaxResult.success(resultMap);
            } else {
                return AjaxResult.error("合同附件上传错误");
            }
        } else {
            return AjaxResult.error("合同附件格式错误");
        }
    }

}
